package com.lagou.sale.config;

import com.lagou.sale.constant.Constant;
import org.apache.rocketmq.client.consumer.DefaultMQPullConsumer;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.tomcat.util.bcel.Const;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class CommonConfig {
    @Value("${redis.server.host}")
    public String host;
    @Value("${redis.server.port}")
    public String port;
    @Value("${rocketmq.name-server}")
    public String nameServer;


    @Bean
    public JedisPool jedisPool() {
        JedisPoolConfig config = new JedisPoolConfig();
        int i = Integer.parseInt(port);
        return new JedisPool(config, host, i);
    }

    @Bean
    public DefaultMQProducer producer() throws MQClientException {
        // 实例化生产者，并指定生产组名称
        DefaultMQProducer producer = new DefaultMQProducer("create_order");
        // 指定name server的地址
        producer.setNamesrvAddr(nameServer);
        // 初始化生产者
        producer.start();
        return producer;
    }

    @Bean ("norCon")
    public DefaultMQPushConsumer normalConsumer() throws MQClientException {
        // 拉取消息的消费者实例化，同时指定消费组名称
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer_order1");
        // 设置name server的地址
        consumer.setNamesrvAddr(nameServer);
        // 对消费者进行初始化，然后就可以使用了
        consumer.subscribe(Constant.OrderTopic, "*");
        return consumer;
    }

    @Bean("timeoutCon")
    public DefaultMQPushConsumer timeoutConsumer() throws MQClientException {
        // 拉取消息的消费者实例化，同时指定消费组名称
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer_order2");
        // 设置name server的地址
        consumer.setNamesrvAddr(nameServer);
        // 对消费者进行初始化，然后就可以使用了
        consumer.subscribe(Constant.OrderTimeoutTopic, "*");
        return consumer;
    }



}
